package com.longrise.android.byjk.plugins.tabfirst;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.common.utils.AppUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/2/21.
 * 头部8个按钮的动态配置
 */

public class HomeRcvheaderAdapter extends RecyclerView.Adapter<HomeRcvheaderAdapter.HomeHeaderHolder> {


    private List<EntityBean> mBeans = new ArrayList<>();
    private OnHeadItemClickListener mListener;

    public HomeRcvheaderAdapter() {

    }

    public void setDatas(EntityBean[] beans) {
        mBeans.clear();
        for (EntityBean bean : beans) {
            mBeans.add(bean);
        }
        notifyDataSetChanged();
    }

    public void setOnItemClickListener(OnHeadItemClickListener listener) {
        this.mListener = listener;
    }


    @Override
    public int getItemCount() {
        return mBeans.size();

    }

    @Override
    public HomeHeaderHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(AppUtil.getContext()).inflate(R.layout.item_home_header_recycle, parent, false);
        HomeHeaderHolder holder = new HomeHeaderHolder(itemView);
        return holder;
    }


    @Override
    public void onBindViewHolder(HomeHeaderHolder holder, final int position) {

        EntityBean entityBean = mBeans.get(position);
        String picpath = entityBean.getString("picpath");
        final String text = entityBean.getString("text");
        final String uniqueid = entityBean.getString("uniqueid");
        final String linkedtype = entityBean.getString("linkedtype");
        final String linkedurl = entityBean.getString("linkedurl");
        String hotlink = entityBean.getString("hotlink");
        Glide.with(AppUtil.getContext()).load(picpath).into(holder.image);
        if (!TextUtils.isEmpty(hotlink)) {
            holder.image_hot.setVisibility(View.VISIBLE);
            Glide.with(AppUtil.getContext()).load(hotlink).into(holder.image_hot);
        }else{
            holder.image_hot.setVisibility(View.GONE);
        }
        holder.text.setText(text);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mListener != null) {
                    mListener.onHeadItemClick(uniqueid, linkedtype, text, linkedurl);
                }

            }
        });
    }

    class HomeHeaderHolder extends RecyclerView.ViewHolder {

        private final ImageView image;
        private final TextView text;
        private final ImageView image_hot;

        public HomeHeaderHolder(View itemView) {
            super(itemView);
            image = (ImageView) itemView.findViewById(R.id.home_fragment_head_rcv_iv);
            text = (TextView) itemView.findViewById(R.id.home_fragment_head_rcv_tv);
            image_hot = (ImageView) itemView.findViewById(R.id.home_fragment_head_rcv_hot);
        }
    }

    interface OnHeadItemClickListener {
        void onHeadItemClick(String uniqueid, String linkedtype, String text, String linkedurl);
    }
}
